package com.skywolf.chem.common.config;

import com.skywolf.chem.common.interceptor.AuthenticationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

//import com.skywolf.chem.common.interceptor.AuthorizationInterceptor;

/**
 * web配置类
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    //    @Resource
//    private AuthorizationInterceptor authorizationInterceptor;
    @Resource
    private AuthenticationInterceptor authenticationInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationInterceptor)
                .excludePathPatterns(
                        "/edu/customer/login/**",
                        "/edu/live/liveback",
                        "/edu/video/**",
                        "/edu/home/**",
                        "/edu/tencent/**",
                        "/edu/comment/commentList",
                        "/edu/live/liveList",
                        "/usercenter/login/**",
                        "/usercenter/user/reset-password/**",
                        "/v2/api-docs",//swagger api json
                        "/swagger-resources/configuration/ui",//用来获取支持的动作
                        "/swagger-resources",//用来获取api-docs的URI
                        "/swagger-resources/configuration/security",//安全选项
                        "/swagger-ui.html",
                        "/webjars/**",
                        "/error",
                        "/",
                        "/csrf",
                        "/news/news/**",
                        "/product/product/**",
                        "/recruitment/recruitment/**",
                        "/home/banner/**",
                        "/sockjs-node/**",
                        "/static/**")
                .addPathPatterns("/**");
    }

    private CorsConfiguration corsConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        /* 请求常用的三种配置，*代表允许所有，当时你也可以自定义属性（比如header只能带什么，只能是post方式等等）
        */
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.setMaxAge(3600L);
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfig());
        return new CorsFilter(source);
    }
}
